package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.KeywordPageInfo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName:KeywordServiceImpl
 * @Description:TODO
 * @author:leiwenjie
 * @date:2022/9/8 17:54
 * @version:1.0
 */
@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketKeyword> query(KeywordPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());


        MarketKeywordExample keywordExample = new MarketKeywordExample();
        keywordExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();

        if (info.getKeyword() != null && !info.getKeyword().isEmpty()) {
            criteria.andKeywordLike("%" + info.getKeyword() + "%");
        }
        if (info.getUrl() != null && !info.getUrl().isEmpty()) {
            criteria.andUrlLike("%" + info.getUrl() + "%");
        }

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(keywordExample);
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(marketKeywords);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public MarketKeyword create(MarketKeyword marketKeyword) {
        marketKeywordMapper.insertSelective(marketKeyword);



        return  marketKeyword;
    }

    @Override
    public void update(MarketKeyword marketKeyword) {
        marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
    }

    @Override
    public void delete(MarketKeyword marketKeyword) {
        marketKeywordMapper.deleteByPrimaryKey(marketKeyword.getId());
    }
}
